package cn.com.franke;

import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <Description> 比较不同json序列化工具类的性能</Description>
 * <ClassName> DiffBetweenJsonTool</ClassName>
 *
 * @author liuxianzhao
 * @date 2018年03月07日 16:11
 */
public class DiffBetweenJsonTool {
    public static void main(String[] args) throws IOException {
        testSerialize();
        System.gc();
        testDeserialize();
    }

    public static void testSerialize() throws IOException {
        //生成较大的json
        List list = new ArrayList();
        DemoBean demoBean = null;
        for (int i = 0; i < 500000; i++) {
            demoBean = new DemoBean(i + "", "name" + i, "100" + i, i);
            list.add(demoBean);
        }
        //1,gson解析
        long start1 = System.currentTimeMillis();
        Gson gson = new GsonBuilder().create();
        String str = gson.toJson(list);
        System.out.println("gson time serialize:" + (System.currentTimeMillis() - start1));
        str = null;
        System.gc();
        //2,jackson解析
        ObjectMapper mapper = new ObjectMapper();
        long start2 = System.currentTimeMillis();
        str = mapper.writeValueAsString(list);
        System.out.println("jackson time serialize:" + (System.currentTimeMillis() - start2));
        str = null;
        System.gc();
        //3.fastJson解析
        long start3 = System.currentTimeMillis();
        str = JSONArray.toJSONString(list);
        System.out.println("fastjson time serialize:" + (System.currentTimeMillis() - start3));
        str = null;
        System.gc();
        //4.JSON-lib解析
//        long start4 = System.currentTimeMillis();
//        str = net.sf.json.JSONArray.fromObject(list).toString();
//        System.out.println("JSON-lib time serialize:" + (System.currentTimeMillis() - start4));
//        str = null;
    }

    public static void testDeserialize() throws IOException {
        //生成较大的json
        List list = new ArrayList();
        DemoBean demoBean = null;
        for (int i = 0; i < 500000; i++) {
            demoBean = new DemoBean(i + "", "name" + i, "100" + i, i);
            list.add(demoBean);
        }

        Gson gson = new GsonBuilder().create();
        String str = gson.toJson(list);

        //1,gson解析
        long start1 = System.currentTimeMillis();
        List l = gson.fromJson(str, List.class);
        System.out.println("gson time deserialize:" + (System.currentTimeMillis() - start1));
        System.out.println(l.size());
        l = null;
        System.gc();
        //2,jackson解析
        ObjectMapper mapper = new ObjectMapper();
        long start2 = System.currentTimeMillis();
        List l2 = mapper.readValue(str, List.class);
        System.out.println("jackson time deserialize:" + (System.currentTimeMillis() - start2));
        System.out.println(l2.size());
        l2 = null;
        System.gc();
        //3.fastJson解析
        long start3 = System.currentTimeMillis();
        List l3 = JSONArray.parseArray(str);
        System.out.println("fastjson time deserialize:" + (System.currentTimeMillis() - start3));
        System.out.println(l3.size());
        l3 = null;
        System.gc();
        //4.JSON-lib解析
        long start4 = System.currentTimeMillis();
        List l4 = net.sf.json.JSONArray.fromObject(str);
        System.out.println("JSON-lib time deserialize:" + (System.currentTimeMillis() - start4));
        System.out.println(l4.size());
    }
}
//gson time deserialize:1260
//500000
//jackson time deserialize:1029
//500000
//fastjson time deserialize:786
//500000
//json-lib超过5分钟

//gson time serialize:2432
//jackson time serialize:522
//fastjson time serialize:895
//json-lib抛异常